.syntax unified
.thumb
.global _start

    .section .isr_vector,"a" @必须加上"a"段属性否则这个section不会被烧写到flash中，why
.word 0
.word 0x080000c9
@下面的中断函数都可以全部注释掉
.word nmi_handler
.word hard_fault_handler
.word memory_management_handler
.word bus_fault_handler
.word usage_fault_handler
.word 0
.word 0
.word 0
.word 0
.word svc_handler
.word debug_monitor_handler
.word 0
.word pendsv_handler
.word systick_handler

    .section .text
    .weak reset_handler
    .type  reset_handler, %function
reset_handler:

    ldr r0, =0x40023830 @enable gpiob clock
    @ ldr r1, [r0]
    @ orr r1, r1, #2
    ldr r1, =0x00000002
    str r1, [r0]

    ldr r0, =0x40020400 @gpiob1 set output mode
    @ ldr r1, [r0]
    @ orr r1, r1, #0x04
    ldr r1, =0x00000004
    str r1, [r0]

    ldr r0, =0x40020414 @gpiob1 set output level
    @ ldr r1, [r0]
    @ orr r1, r1, #2
    ldr r1, =0x00000000
    str r1, [r0]

    bl relocate_text
    bl relocate_data

    ldr sp, =_estack
    bl main
loop:
    b loop

.weak relocate_text
.weak relocate_data

relocate_text:
    ldr r0, =text_base
    ldr r1, =0x150
    ldr r3, =0x20000000
    mov r2, #0

impl_reloacate_text:
    ldr r4, [r0]
    str r4, [r3]
    add r2, r2, #4
    add r3, r3, r2
    add r0, r0, r2
    cmp r2, r1
    bne impl_reloacate_text
    bx lr

relocate_data:
    ldr r0, = _sdata
    ldr r1, = _edata
    ldr r2, =_sidata
    mov r3, #0

impl_reloacate_data:
    ldr r4, [r2]
    str r4, [r0]
    add r3, r3, #4
    add r2, r2, r3
    add r0, r0, r3
    cmp r0, r1
    bmi impl_reloacate_data

    bx lr